// 简单的Markdown到HTML转换器
function markdownToHtml(markdown) {
  let html = markdown
    // 标题
    .replace(/^### (.*$)/gim, '<h3>$1</h3>')
    .replace(/^## (.*$)/gim, '<h2>$1</h2>')
    .replace(/^# (.*$)/gim, '<h1>$1</h1>')
    // 粗体
    .replace(/\*\*(.*?)\*\*/gim, '<strong>$1</strong>')
    // 斜体
    .replace(/\*(.*?)\*/gim, '<em>$1</em>')
    // 代码
    .replace(/`(.*?)`/gim, '<code>$1</code>')
    // 链接
    .replace(/\[([^\]]+)\]\(([^\)]+)\)/gim, '<a href="$2">$1</a>')
    // 图片
    .replace(/!\[([^\]]*)\]\(([^\)]+)\)/gim, '<img alt="$1" src="$2" />')
    // 换行
    .replace(/\n\n/gim, '</p><p>')
    .replace(/\n/gim, '<br />');
  
  // 处理列表
  html = html.replace(/^- (.*$)/gim, '<li>$1</li>');
  html = html.replace(/(<li>.*<\/li>)/gims, '<ul>$1</ul>');
  
  // 处理有序列表
  html = html.replace(/^\d+\. (.*$)/gim, '<li>$1</li>');
  
  // 处理引用
  html = html.replace(/^> (.*$)/gim, '<blockquote>$1</blockquote>');
  
  // 包装段落
  if (html && !html.startsWith('<')) {
    html = '<p>' + html + '</p>';
  }
  
  return html;
}

// 复制到剪贴板
async function copyToClipboard(text) {
  try {
    await navigator.clipboard.writeText(text);
    return true;
  } catch (err) {
    // 降级方案
    const textArea = document.createElement('textarea');
    textArea.value = text;
    document.body.appendChild(textArea);
    textArea.select();
    const success = document.execCommand('copy');
    document.body.removeChild(textArea);
    return success;
  }
}

// 显示通知
function showNotification(message, type = 'success') {
  const notification = document.createElement('div');
  notification.style.cssText = `
    position: fixed;
    top: 20px;
    right: 20px;
    padding: 12px 20px;
    background: ${type === 'success' ? '#4caf50' : '#f44336'};
    color: white;
    border-radius: 6px;
    z-index: 1000;
    font-size: 14px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.2);
  `;
  notification.textContent = message;
  document.body.appendChild(notification);
  
  setTimeout(() => {
    notification.remove();
  }, 3000);
}

// 监听来自background script的消息
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if (request.action === 'displayResult') {
    const originalHtmlElement = document.getElementById('originalHtml');
    const markdownResultElement = document.getElementById('markdownResult');
    const markdownPreviewElement = document.getElementById('markdownPreview');
    
    // 显示原始HTML
    originalHtmlElement.textContent = request.originalHtml;
    
    // 显示Markdown结果
    markdownResultElement.textContent = request.markdown;
    
    // 显示Markdown预览
    const previewHtml = markdownToHtml(request.markdown);
    markdownPreviewElement.innerHTML = previewHtml;
    
    // 存储数据供复制使用
    window.markdownData = request.markdown;
    window.htmlData = request.originalHtml;
  }
});

// 页面加载完成后设置事件监听器
document.addEventListener('DOMContentLoaded', function() {
  const copyMarkdownBtn = document.getElementById('copyMarkdown');
  const copyHtmlBtn = document.getElementById('copyHtml');
  
  copyMarkdownBtn.addEventListener('click', async function() {
    if (window.markdownData) {
      const success = await copyToClipboard(window.markdownData);
      if (success) {
        showNotification('Markdown 已复制到剪贴板');
      } else {
        showNotification('复制失败，请重试', 'error');
      }
    }
  });
  
  copyHtmlBtn.addEventListener('click', async function() {
    if (window.htmlData) {
      const success = await copyToClipboard(window.htmlData);
      if (success) {
        showNotification('HTML 已复制到剪贴板');
      } else {
        showNotification('复制失败，请重试', 'error');
      }
    }
  });
});